<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="支番" prop="branchNum">
      <el-input v-model="dataForm.branchNum" placeholder="支番"></el-input>
    </el-form-item>
    <el-form-item label="工序编号" prop="workingProcedureNum">
      <el-input v-model="dataForm.workingProcedureNum" placeholder="工序编号"></el-input>
    </el-form-item>
    <el-form-item label="完成日" prop="completeDate">
      <el-input v-model="dataForm.completeDate" placeholder="完成日"></el-input>
    </el-form-item>
    <el-form-item label="日期调整次数" prop="adjustmentCnt">
      <el-input v-model="dataForm.adjustmentCnt" placeholder="日期调整次数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          internalEnterpriseNum: 0,
          branchNum: '',
          workingProcedureNum: '',
          completeDate: '',
          adjustmentCnt: ''
        },
        dataRule: {
          branchNum: [
            { required: true, message: '支番不能为空', trigger: 'blur' }
          ],
          workingProcedureNum: [
            { required: true, message: '工序编号不能为空', trigger: 'blur' }
          ],
          completeDate: [
            { required: true, message: '完成日不能为空', trigger: 'blur' }
          ],
          adjustmentCnt: [
            { required: true, message: '日期调整次数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.internalEnterpriseNum = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.internalEnterpriseNum) {
            this.$http({
              url: this.$http.adornUrl(`/schedule/mesproductionschedulingdetails/info/${this.dataForm.internalEnterpriseNum}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.branchNum = data.mesproductionschedulingdetails.branchNum
                this.dataForm.workingProcedureNum = data.mesproductionschedulingdetails.workingProcedureNum
                this.dataForm.completeDate = data.mesproductionschedulingdetails.completeDate
                this.dataForm.adjustmentCnt = data.mesproductionschedulingdetails.adjustmentCnt
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/schedule/mesproductionschedulingdetails/${!this.dataForm.internalEnterpriseNum ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'internalEnterpriseNum': this.dataForm.internalEnterpriseNum || undefined,
                'branchNum': this.dataForm.branchNum,
                'workingProcedureNum': this.dataForm.workingProcedureNum,
                'completeDate': this.dataForm.completeDate,
                'adjustmentCnt': this.dataForm.adjustmentCnt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
